package object;

import object.Commit;
import object.Repository;
import object.HEAD;

import java.io.*;

public class Branch implements Serializable {
    
    protected String branchName = "master";
    private Commit commit;

    public String getBranchName() {
        return branchName;
    }
    public Commit getCommit() { return this.commit; }


    public Branch() {};
    public Branch(String branchName) throws IOException {
        this.branchName = branchName;
        this.commit = new Commit();
        this.writeObject();
    }

    public void setCommit(Commit commit){
        this.commit = commit;
    }

    public void writeObject() throws IOException {
        File branchFile = new File(".jit" + File.separator + "refs" + File.separator + "heads" + File.separator + branchName);
        try {

            ObjectOutputStream out =
                    new ObjectOutputStream(new FileOutputStream(branchFile));
            out.writeObject(this);
            out.close();
        }
        catch(Exception ex){
            ex.getStackTrace();
        }
    }
    public static Branch deserialize(String branchName) throws Exception{
        try{
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(".jit" + File.separator + "refs" + File.separator + "heads" + File.separator + branchName));
            return (Branch) in.readObject();
        }
        catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }



}